package JPetStore.service;

import JPetStore.domain.*;
import JPetStore.persistence.*;
import JPetStore.persistence.impl.*;

import java.util.List;

public class OrderService {
    private ItemDao itemDAO = new ItemDaoImpl();
    private LineItemDao lineItemDAO = new LineItemDaoImpl();
    private OrderDao orderDAO = new OrderDaoImpl();
    public OrderService() {
    }

    public List<Order> getOrdersByUsername(String username) {
        return this.orderDAO.getOrdersByUsername(username);
    }

    public Order getOrder(int orderId) {
        Order order = this.orderDAO.getOrderByOrderId(orderId);

        order.setLineItems(this.lineItemDAO.getLineItemsByOrderId(orderId));

        for(int i = 0; i < order.getLineItems().size(); ++i) {
            LineItem lineItem = (LineItem)order.getLineItems().get(i);
            Item item = this.itemDAO.getItem(lineItem.getItemId());
            item.setQuantity(this.itemDAO.getInventoryQuantity(lineItem.getItemId()));
            lineItem.setItem(item);
        }

        return order;
    }
    public int getLatestOrderId(){
        return this.orderDAO.getLatestOrderId();
    }

    public void insertOrder(Account account, Order order){
        this.orderDAO.insertOrder(account,order);
    }

}
